/**
 * 
 */
package net.cloudcodex.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasMouseOverHandlers;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.DockPanel.DockLayoutConstant;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

/**
 * @author Daniel This Widget is a verticalPanel that display an "Image" widget
 *         with a tilte below
 */
public abstract class Avatar extends FlowPanel implements HasClickHandlers,
		HasMouseOverHandlers  {

	private static final String AVATAR_IMAGE_STYLENAME = "avatarImage";
	protected String MAX_IMAGE_HEIGHT = "100";
	protected String MAX_IMAGE_WIDTH = "100";
	private final DockPanel avatarDockPanel = new DockPanel();
	private String imageURL;
	private String name;
	private Long id ;

	public String getName() {
		return name;
	}

	private String defaultImageURL;
	private final Image image = new Image();
	private final Label iconeLabel = new Label();
	private boolean withName;
	
	
	public Avatar() {
		this(null,null);
	}

	public Avatar(String imageURL, String name) {
		setImageURL(imageURL);
		setName(name);
	}

	/**
	 * Initialise the Widget
	 */
	protected void init() {
		if (imageURL != null && imageURL != "") {
			image.setUrl(imageURL);
		} else {
			image.setUrl(defaultImageURL);
		}
		avatarDockPanel.add(image,DockPanel.CENTER);

		if (withName) {
			if (name != null && name != "") {
				iconeLabel.setText(name);
			} else {
				iconeLabel.setText("FIXME");
			}
			this.labelPosition(DockPanel.SOUTH);
		}
		image.addStyleName(AVATAR_IMAGE_STYLENAME);
		iconeLabel.addStyleName("avatarLabel");
		resize(MAX_IMAGE_HEIGHT, MAX_IMAGE_WIDTH);
		this.add(avatarDockPanel);
		
	}

	/**
	 * set the url image
	 * @param imageURL
	 */
	public void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}
	/**
	 * set the name of this avatar
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Set the default url
	 */
	protected abstract void defaultImageURL();

	public String getDefaultImageURL() {
		return defaultImageURL;
	}

	public void setDefaultImageURL(String defaultImageURL) {
		this.defaultImageURL = defaultImageURL;
	}

	public void setIconeStyleName(String stylename) {
		image.addStyleName(stylename);
	}
	
	/**
	 * set the position of the label for this avatar
	 * (Direction can only be a DockPanel position) 
	 * @param direction the DockLayoutConstant constant ( NORTH, SOUTH, EAST, WEST, CENTER)
	 */
	public void labelPosition(DockLayoutConstant direction){
		avatarDockPanel.remove(iconeLabel);
		avatarDockPanel.add(iconeLabel,direction);
	}

	public void setNameStyleName(String stylename) {
		iconeLabel.addStyleName(stylename);
	}

	public Image getImage() {
		return image;
	}

//	public HandlerRegistration addClickHandler(ClickHandler handler) {
//		return image.addClickHandler(handler);
//	}
//
//	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
//		return image.addMouseOverHandler(handler);
//	}

	public void setWithName(boolean withName) {
		this.withName = withName;
	}

	public void resize(String width, String height) {
		int originalHeight = image.getOffsetHeight();
		int originalWidth = image.getOffsetWidth();
		if (originalHeight > originalWidth) {
			image.setHeight(width + "px");
		} else {
			image.setWidth(height + "px");
		}
	}
	
	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		return addDomHandler(handler, ClickEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		 return addDomHandler(handler, MouseOverEvent.getType());
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public void removeAvatarStyleName(){
		image.removeStyleName(AVATAR_IMAGE_STYLENAME);
	}
}
